package top.aiprogram.common.page;

import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Created by HP on 2018/10/17.
 * 这是一个分页类,是通用的,因此需要的时候,复制一个就已经足够了,不需要
 * 仔细琢磨怎么写的,没有研究价值
 */

public class PageQueryBean {
    private static final  int DEFAULT_PAGE_SIZE=10;
    //当前页
    private  Integer currentPage;
    //每一页放多少条数据
    private Integer pageSize;
    //总共有多少行
    private Integer totalRows;
    //从哪一行开始查询呢
    private Integer startRows;
    //总共有多少页呢
    private Integer totalPage;
    //这里使用泛型通配符,可以装任何类型的返回的数据集
    private List<?> items;

    public final Integer getStartRows(){
        if (startRows==null){
            startRows=getCurrentPage()==1?0:(getCurrentPage()-1)*getPageSize();
        }
        return startRows;
    }
    public void setStartRows(Integer startRows){
        this.startRows=startRows;
    }

    public Integer getPageSize() {
        return pageSize==null?DEFAULT_PAGE_SIZE:pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalRows() {
        return totalRows;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public void setTotalRows(Integer totalRows) {
        this.totalRows = totalRows;
        int totalPage=totalRows%getPageSize()==0?(totalRows/getPageSize()):(totalRows/getPageSize()+1);
        setTotalPage(totalPage);
    }
    public List<?> getItems(){
        return items==null? Collections.EMPTY_LIST:items;
    }

    public void setItems(List<?> items) {
        this.items = items;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getCurrentPage() {
        return currentPage==null?1:currentPage;
    }

    public Integer getTotalPage() {
        return totalPage==null?1:totalPage;
    }

    @Override
    public String toString() {
        return "PageQueryBean [currentPage=" + currentPage + ", pageSize="
                + pageSize + ", totalRows=" + totalRows + ", startRow="
                + startRows + ", totalPage=" + totalPage + ", items=" + items
                + "]";
    }
}
